package com.zy.mall.service.impl;

import com.zy.mall.dao.UserDAO;
import com.zy.mall.entity.User;
import com.zy.mall.service.UserService;
import com.zy.mall.utils.MD5Utils;
import com.zy.mall.po.ResStatus;
import com.zy.mall.po.ResultVo;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

@Service
@Scope("singleton")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Transactional
    public ResultVo userRegist(String name, String pwd) {
        synchronized (this) {
            // 判断该用户名是否被注册
            User user = userDAO.queryUserByName(name);

            if (user == null) {
                // 加密 密码
                String md5Pwd = MD5Utils.md5(pwd);

                user = new User();
                user.setUsername(name);
                user.setPassword(md5Pwd);
                user.setUserImg("img/default.png");
                user.setUserRegtime(new Date());
                user.setUserModtime(new Date());
                int i = userDAO.insertUseGeneratedKeys(user);

                if (i > 0) {
                    return new ResultVo(ResStatus.OK, "注册成功!", user);
                } else {
                    return new ResultVo(ResStatus.NO, "注册失败!", null);
                }
            } else {
                return new ResultVo(ResStatus.NO, "用户名已被注册!", null);
            }
        }
    }

    @Override
    public ResultVo checkLogin(String name, String pwd) {
//        Example example = new Example(User.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("username", name);
//        List<User> users = userDAO.selectByExample(example);

        // 查询用户信息
        User user = userDAO.queryUserByName(name);

        if (user == null){
            // 用户名不存在   返回出错数据
            return new ResultVo(ResStatus.NO, "登录失败, 用户名不存在！", null);
        }else{
            // 密码解密 并校验
            String md5Pwd = MD5Utils.md5(pwd);
            if (md5Pwd.equals(user.getPassword())){
                // 验证成功 生成token
                JwtBuilder builder = Jwts.builder();

                HashMap<String,Object> map = new HashMap<>();
                map.put("key1","value1");
                map.put("key2","value2");

                String token = builder.setSubject(name)                     //主题，就是token中携带的数据
                        .setIssuedAt(new Date())                            //设置token的生成时间
                        .setId(user.getUserId() + "")                       //设置用户id为token  id
                        .setClaims(map)                                     //map中可以存放用户的角色权限信息
                        .setExpiration(new Date(System.currentTimeMillis() + 9*60*1000)) //设置token过期时间
                        .signWith(SignatureAlgorithm.HS256, "123456")     //设置加密方式和加密密码
                        .compact();

                return new ResultVo(ResStatus.OK, token, user);
            }else{
                // 密码错误
                return new ResultVo(10001, "登陆失败, 密码错误!", null);
            }
        }
    }
}
